package com.ttpai.boot.lbs.controller;

import com.ttpai.boot.lbs.dao.MetroPointDAO;
import com.ttpai.boot.lbs.model.MetroPointRawVO;
import com.ttpai.boot.lbs.service.MetroPointPlanRouteService;
import com.ttpai.boot.lbs.service.MetroPointRawService;
import com.ttpai.boot.lbs.service.RoutePlanReportService;
import com.ttpai.boot.lbs.util.DateUtil;
import com.ttpai.boot.lbs.util.UrlUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Created by kail on 2017/2/25.
 */
@Controller
@RequestMapping("/metro")
public class MetroController {

    @Autowired
    private RoutePlanReportService routePlanReportService;

    @Autowired
    private MetroPointPlanRouteService metroPointPlanRouteService;
    @Autowired
    private MetroPointRawService metroPointRawService;

    @RequestMapping("/show_all_point")
    public String showAllPoint(
            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "lineName", required = false) String lineName,
            @RequestParam(value = "feature", required = false) Integer feature,
            Model model) {

        List<MetroPointRawVO> metroPointVOS = metroPointRawService.selectMetroPoint(city, lineName);
        Map<String, Integer> baiduMetorCity = metroPointRawService.selectBaiduMetorCity();

        model.addAttribute("metroPoint", metroPointVOS);
        model.addAttribute("baiduMetorCity", baiduMetorCity);

        if (null != feature) {
            if (1 == feature) {
                return "redirect:/metro/point_fix?city=" + UrlUtil.encode(city) + "&lineName=" + UrlUtil.encode(lineName);
            }
            if (2 == feature) {
                System.out.println(city);
                if (null == metroPointVOS || metroPointVOS.isEmpty()) {
                    Integer code = baiduMetorCity.get(city);
                    System.out.println(code);
                    if (null != city && null != code) {
                        System.out.println("insertBaiduMetroCity");
                        metroPointRawService.insertBaiduMetroCity(code, city);
                    }
                    model.addAttribute("baiduMetorCity", metroPointRawService.selectMetroPoint(city, lineName));
                }
            }

            if (3 == feature) {
                if (StringUtils.isNotBlank(city)) {
                    metroPointRawService.updateMetroLngLat(city);
                }
            }
        }

        return "metro/show_all_point";
    }

    @RequestMapping("/point_fix")
    public String pointFix(
            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "lineName", required = false) String lineName,
            Model model) {

        List<MetroPointRawVO> metroPointVOS = metroPointRawService.selectMetroPoint(city, lineName);
        model.addAttribute("metroPoint", metroPointVOS);

        return "metro/point_fix";
    }

    @RequestMapping("/near_metro")
    public String findMetro(@RequestParam(value = "city", required = false) String city, @RequestParam(value = "date", required = false) String dateStr, Model model) {
        if (StringUtils.isNotBlank(city) && StringUtils.isNotBlank(dateStr)) {
            model.addAttribute("nearMetros", metroPointPlanRouteService.selectNearMetro(DateUtil.format(dateStr), city));
        }
        model.addAttribute("dates", routePlanReportService.selectHaveLLDayNotZero());
        model.addAttribute("cityStr", MetroPointDAO.ALL_CITY);

        return "metro/near_metro";
    }

}
